package com.cloudbroker.bcs.common.configure;

import java.util.HashMap;
import java.util.Map;

import com.cloudbroker.bcs.common.configure.constants.ConfigureConstants;
import com.cloudbroker.bcs.common.configure.zookeeper.ZooKeeperConfigurator;
import com.cloudbroker.bcs.common.zookeeper.ZooKeeperClient;

public class HSBCSZooKeeperConfigurator extends ZooKeeperConfigurator {
    
    public static final String EVT_RELOAD_SUFFIX = "ConfigReloaded";
    
    private static final String PREFIX = "biz";
    
    private static final String CONFIGURATIONS = "configurations";
    
    private static final String CONFIG_ROOT_PATH = ZooKeeperClient.PATH_SEPARATOR_CHAR + PREFIX
            + ZooKeeperClient.PATH_SEPARATOR_CHAR + CONFIGURATIONS;
    
    private static Map<String, String> categoryConfigurationRootPathMap = new HashMap<String, String>();
    
    public HSBCSZooKeeperConfigurator(ZooKeeperClient client) {
        super(client);
    }
    
    /*
     * 
     * {
     * jdbc : {
     * db.driverClassName : "",
     * db.url : "",
     * db.username : "",
     * db.password : ""
     * }
     * 
     * abc : {
     * 
     * }
     * }
     * 
     */
    
    // /biz/configurations/db/tenantId
    // /biz/configurations/redis/tenantId
    
    public static String getConfigRootPath() {
        return CONFIG_ROOT_PATH;
    }
    
    public static String getCategoryConfigurationRootPath(String category) {
        String path = categoryConfigurationRootPathMap.get(category);
        if (null == path) {
            path = createCategoryConfigurationRootPath(category);
            categoryConfigurationRootPathMap.put(category, path);
        }
        return path;
    }
    
    public static String createCategoryConfigurationRootPath(String category) {
        StringBuilder sBuilder = new StringBuilder(32);
        sBuilder.append(getConfigRootPath()).append(ZooKeeperClient.PATH_SEPARATOR_CHAR).append(category);
        return sBuilder.toString();
    }
    
    public class HSBCSConfigReader extends ConfigReader {
        
        protected String category;
        
        public HSBCSConfigReader(String category) {
            super(createCategoryConfigurationRootPath(category));
            this.category = category;
        }
        
        @Override
        public HSBCSConfigReader onPath(String tenantId, String... concernFlags) {
            if (null == concernFlags || 0 == concernFlags.length) {
                concernFlags = new String[] { category, tenantId };
            }
            return (HSBCSConfigReader) super.onPath(tenantId, concernFlags);
        }
        
        @Override
        public HSBCSConfigReader onRoot() {
            concernFlags = new String[] { category, ConfigureConstants.TENANT_ID_NO_TENANT };
            return (HSBCSConfigReader) super.onRoot();
        }
        
        @Override
        public HSBCSConfigReader noMerge() {
            return (HSBCSConfigReader) super.noMerge();
        }
        
        public String getCategory() {
            return category;
        }
        
    }
    
    public class HSBCSConfigWriter extends ConfigWriter {
        
        protected String category;
        
        public HSBCSConfigWriter(String category) {
            super(createCategoryConfigurationRootPath(category));
            this.category = category;
        }
        
        @Override
        public HSBCSConfigWriter onPath(String tenantId) {
            return (HSBCSConfigWriter) super.onPath(tenantId);
        }
        
        @Override
        public HSBCSConfigWriter onRoot() {
            return (HSBCSConfigWriter) super.onRoot();
        }
        
        @Override
        public HSBCSConfigWriter overwrite() {
            return (HSBCSConfigWriter) super.overwrite();
        }
        
        public String getCategory() {
            return category;
        }
        
    }
    
    public HSBCSConfigReader getHSBCSConfigReader(String category) {
        return new HSBCSConfigReader(category).onRoot();
    }
    
    public HSBCSConfigReader getHSBCSConfigReader(String category, String tenantId) {
        HSBCSConfigReader reader = new HSBCSConfigReader(category);
        if (ConfigUtil.isNoTenant(tenantId)) {
            return reader.onRoot();
        } else {
            return reader.onPath(tenantId);
        }
    }
    
    public HSBCSConfigWriter getHSBCSConfigWriter(String category) {
        return new HSBCSConfigWriter(category).onRoot().overwrite();
    }
    
    public HSBCSConfigWriter getHSBCSConfigWriter(String category, String tenantId) {
        HSBCSConfigWriter writer = new HSBCSConfigWriter(category).overwrite();
        if (ConfigUtil.isNoTenant(tenantId)) {
            return writer.onRoot();
        } else {
            return writer.onPath(tenantId);
        }
    }
    
}
